const app = getApp()
import mqtt from "../../components/utils/mqtt.min"

Page({
  data: {
    key: "bb363083dafa44d48a449ce0c1c27aef",// 用户密钥
    ledtopic: "reciot",// 控制LED的主题
    dhttopic: "temp004",
    mqtt_user: "iot601_wu",
    mqtt_pwd: "1@3fS6",
    device_status: "离线", // MQTT连接状态
    powerstatus: "关闭", // LED状态  
  },
  client: null, // MQTT客户端  
  onLoad: function () {
    this.mqttConnect();
  },
  // MQTT SETTING ---------------------------------------------------- START
  mqttConnect() {
    var that = this  //this用于上下文的引用，this通常指向当前对象的实例
    let ip='bemfa.com:9504/wss'
    // MQTT连接的配置  
    var ESP8266_options = {
      keepalive: 60,// 60s，表示心跳间隔
      ssl: false,
      clean: true,// cleanSession不保持持久会话
      protocolVersion: 4,// MQTT v3.1.1
      clientId: this.data.key,//clientID
      // username: this.data.mqtt_user,
      // password: this.data.mqtt_pwd,
      reconnectPeriod: 4 * 1000,
      connecTimeout: 3 * 1000,
    }
    // 初始化mqtt连接
    this.data.client = mqtt.connect('wxs://'+ip, ESP8266_options)
    // 连接MQTT服务器
    this.data.client.on("connect", function () {
      console.log("连接成功")
      that.setData({device_status: "在线"})
      // 订阅LED状态主题
      that.data.client.subscribe(that.data.ledtopic, function (err) {
        if (err) {
          console.log(err)  //err如果为真表示订阅失败
        }
      })
      //订阅dht11温湿度主题
      that.data.client.subscribe(that.data.dhttopic, function (err) {
        if (err) {
          console.log(err)
        }
      })
    })
    // 接收消息
    this.data.client.on('message', function (topic, message) {
      //在 console窗口输出主题
      console.log(topic);
      var msg = message.toString()
      if (topic == that.data.ledtopic) {
        that.setData({
          powerstatus: msg === 'on' ? "打开" : "关闭"
        });
      }
    })
    // 断线重连
    this.data.client.on("reconnect", function () {
      console.log("重新连接")
      that.setData({device_status: "重新连接"})
    })
    // 连接失败
    this.data.client.on('error', function (err) {
      console.log("链接失败", err)
      that.setData({
        device_status: "重新连接"
      })
    })
    // 连接断开
    this.data.client.on('end', function () {
      console.log("连接断开")
      that.setData({
        device_status: "连接断开"
      })
    })
  },
  // MQTT SETTING ---------------------------------------------------- END
  openclick: function () {
    if (this.data.client.connected) {
      this.data.client.publish(this.data.ledtopic, 'ON')
      this.setData({powersatus: "打开"})
    } else {
      console.log("MQTT 客户端错误，发送信息失败")
    }
  },

  closeclick: function () {
    if (this.data.client.connected) {
      this.data.client.publish(this.data.ledtopic, 'OFF')
      this.setData({ powerstatus: "关闭", })
    } else {
      console.log("MQTT 客户端错误，发送信息失败")
    }
  },
  toggleLED: function () {
    if (this.data.client.connected) {
      let newStatus = this.data.client.powerstatus === '打开' ? "关闭" : "打开"
      let message = newStatus === "打开" ? "ON" : "OFF"
      this.data.client.publish(this.data.ledtopic, message)
      this.setData({powerstatus: newStatus, })
    } else {
      console.log("MQTT 客户端错误，发送信息失败")
    }
  }
})